# 1. 使用代码来实现的B站发布弹幕的案例
#
#     <1>能够实现无限发送弹幕
#
#     <2>能够实现对不同主播发送弹幕
#     <3>哔哩哔哩直播(随意挑选主播)
import time

import requests

def bili(msg):
    url = "https://api.live.bilibili.com/msg/send"
    headers = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36 Edg/111.0.1661.41',
        'cookie': 'buvid3=9281B483-890E-734B-CEC7-8742973DEB7D35893infoc; b_nut=1676613735; CURRENT_FNVAL=4048; _uuid=2181264A-5477-5D38-3CAB-13FBA42D313C39830infoc; buvid4=B24D4EF4-4E9B-4ADE-BD1B-6D1DEBA9C89136912-023021714-ytR2gTyIVaYpBJKBBB9VOA%3D%3D; buvid_fp=9cbca021c496cdcef5b069c503e21a14; rpdid=0zbfvShdS5|MFnUcokL|45T|3w1PD9uS; i-wanna-go-back=-1; b_lsid=102297664_186F331FC3D; bsource=search_bing; header_theme_version=CLOSE; home_feed_column=4; SESSDATA=1317b614%2C1694669444%2Cb1238%2A31; bili_jct=7e33cae7c51bc229c93decddd001a6e0; DedeUserID=2015483138; DedeUserID__ckMd5=fe41c156cb9b1c55; sid=6837cfku; is-2022-channel=1; LIVE_BUVID=AUTO4016791175032153; _dfcaptcha=7dc5e8d34b4a025b103240233fedcca3; innersign=0; b_ut=5; Hm_lvt_8a6e55dbd2870f0f5bc9194cddf32a02=1679117971; Hm_lpvt_8a6e55dbd2870f0f5bc9194cddf32a02=1679118272; PVID=14'
    }
    data= {''
    'bubble': '0',
    'msg': msg,
    'color': '16777215',
    'mode': '1',
    'fontsize': '25',
    'rnd': int(time.time()),
    # 主播房间号
    'roomid': '27148662',
    'csrf': '7e33cae7c51bc229c93decddd001a6e0',
    'csrf_token': '7e33cae7c51bc229c93decddd001a6e0'
}
    # 发送弹幕的包
    r = requests.post(url,headers=headers,data=data).text
    print(r)

if __name__ == '__main__':
    while 1:
        msg_list = ['相信公安相信党','恋爱没有好下场','丢掉幻想','脚踏实地','创造未来','花若盛开','蝴蝶自来']
        for msg in msg_list:
            bili(msg)
            time.sleep(1)